<?php
// 引入头部
require_once 'includes/header.php';

// 获取今日统计数据
$today = date('Y-m-d');
$today_start = $today . ' 00:00:00';
$today_end = $today . ' 23:59:59';

// 今日订单数
$total_orders = Database::fetch("SELECT COUNT(*) as count FROM orders WHERE created_at BETWEEN ? AND ?", [$today_start, $today_end]);
$total_orders = $total_orders ? $total_orders['count'] : 0;

// 今日销售额
$total_sales = Database::fetch("SELECT SUM(actual_price) as total FROM orders WHERE status != 'cancelled' AND created_at BETWEEN ? AND ?", [$today_start, $today_end]);
$total_sales = $total_sales && $total_sales['total'] ? $total_sales['total'] : 0;

// 今日会员消费
$member_sales = Database::fetch("SELECT SUM(actual_price) as total FROM orders WHERE status != 'cancelled' AND user_id IS NOT NULL AND created_at BETWEEN ? AND ?", [$today_start, $today_end]);
$member_sales = $member_sales && $member_sales['total'] ? $member_sales['total'] : 0;

// 待处理订单
$pending_orders = Database::fetch("SELECT COUNT(*) as count FROM orders WHERE status IN ('paid', 'cooking') AND created_at BETWEEN ? AND ?", [$today_start, $today_end]);
$pending_orders = $pending_orders ? $pending_orders['count'] : 0;

// 获取最近10个订单
$recent_orders = Database::fetchAll("SELECT o.*, u.name as member_name 
                                    FROM orders o 
                                    LEFT JOIN users u ON o.user_id = u.id 
                                    ORDER BY o.created_at DESC LIMIT 10");
?>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-3 col-sm-6 mb-4">
            <div class="card">
                <div class="card-body text-center">
                    <h1 class="display-4 text-primary"><?php echo $total_orders; ?></h1>
                    <p class="lead">今日订单数</p>
                </div>
            </div>
        </div>
        
        <div class="col-md-3 col-sm-6 mb-4">
            <div class="card">
                <div class="card-body text-center">
                    <h1 class="display-4 text-success">¥<?php echo number_format($total_sales, 2); ?></h1>
                    <p class="lead">今日销售额</p>
                </div>
            </div>
        </div>
        
        <div class="col-md-3 col-sm-6 mb-4">
            <div class="card">
                <div class="card-body text-center">
                    <h1 class="display-4 text-warning">¥<?php echo number_format($member_sales, 2); ?></h1>
                    <p class="lead">会员消费</p>
                </div>
            </div>
        </div>
        
        <div class="col-md-3 col-sm-6 mb-4">
            <div class="card">
                <div class="card-body text-center">
                    <h1 class="display-4 text-danger"><?php echo $pending_orders; ?></h1>
                    <p class="lead">待处理订单</p>
                </div>
            </div>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">最近订单</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>订单号</th>
                                    <th>餐号</th>
                                    <th>金额</th>
                                    <th>支付方式</th>
                                    <th>状态</th>
                                    <th>时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (empty($recent_orders)): ?>
                                <tr>
                                    <td colspan="7" class="text-center">暂无订单数据</td>
                                </tr>
                                <?php else: ?>
                                <?php foreach ($recent_orders as $order): ?>
                                <tr>
                                    <td><?php echo htmlspecialchars($order['order_no']); ?></td>
                                    <td><?php echo htmlspecialchars($order['queue_number']); ?></td>
                                    <td>¥<?php echo number_format($order['actual_price'], 2); ?></td>
                                    <td>
                                        <?php
                                        switch ($order['payment_method']) {
                                            case 'cash': echo '现金'; break;
                                            case 'balance': echo '余额'; break;
                                            case 'wechat': echo '微信'; break;
                                            case 'alipay': echo '支付宝'; break;
                                            default: echo $order['payment_method']; break;
                                        }
                                        ?>
                                    </td>
                                    <td>
                                        <?php
                                        switch ($order['status']) {
                                            case 'pending': 
                                                echo '<span class="badge badge-warning">待支付</span>'; 
                                                break;
                                            case 'paid': 
                                                echo '<span class="badge badge-primary">已支付</span>'; 
                                                break;
                                            case 'cooking': 
                                                echo '<span class="badge badge-info">制作中</span>'; 
                                                break;
                                            case 'completed': 
                                                echo '<span class="badge badge-success">已完成</span>'; 
                                                break;
                                            case 'cancelled': 
                                                echo '<span class="badge badge-danger">已取消</span>'; 
                                                break;
                                            default: 
                                                echo '<span class="badge badge-secondary">' . $order['status'] . '</span>'; 
                                                break;
                                        }
                                        ?>
                                    </td>
                                    <td><?php echo date('m-d H:i', strtotime($order['created_at'])); ?></td>
                                    <td>
                                        <a href="/POS/order_detail.php?id=<?php echo $order['id']; ?>" class="btn btn-sm btn-primary">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-md-4">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">快捷操作</h5>
                </div>
                <div class="card-body">
                    <div class="d-grid gap-2">
                        <a href="/POS/products.php" class="btn btn-lg btn-primary mb-3">
                            <i class="fas fa-shopping-cart mr-2"></i> 开始点餐
                        </a>
                        <a href="/POS/orders.php" class="btn btn-lg btn-info mb-3">
                            <i class="fas fa-clipboard-list mr-2"></i> 查看订单
                        </a>
                        <a href="/POS/members.php" class="btn btn-lg btn-success mb-3">
                            <i class="fas fa-users mr-2"></i> 会员管理
                        </a>
                        <?php if ($current_user && $current_user['role'] === 'admin'): ?>
                        <a href="/POS/reports.php" class="btn btn-lg btn-warning">
                            <i class="fas fa-chart-bar mr-2"></i> 销售报表
                        </a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">系统信息</h5>
                </div>
                <div class="card-body">
                    <ul class="list-group">
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            当前用户
                            <span><?php echo htmlspecialchars($current_user['name']); ?> (<?php echo htmlspecialchars($current_user['role'] === 'admin' ? '管理员' : '收银员'); ?>)</span>
                        </li>
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            系统时间
                            <span><?php echo date('Y-m-d H:i:s'); ?></span>
                        </li>
                        <li class="list-group-item d-flex justify-content-between align-items-center">
                            系统版本
                            <span>v1.0.0</span>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

<?php
// 引入页脚
require_once 'includes/footer.php';
?> 